package game;

import javax.microedition.lcdui.Graphics;


/**
 * LUMM2 Game Template Game Class
 * 
 * @author IMS
 * @since 18.02.2009
 */

public class LUMM2_Game {
	
	/** gamestate for choosing sound/volume/language etc. */
	public final static byte SETTINGS = 0;

	/** gamestate for playing the into video with skip function */
	public final static byte INTRO = 1;

	/** gamestate for displaying the SVG menu */
	public final static byte MENU = 2;

	/** state while playing the game */
	public final static byte PLAY = 3;

	/** gamestate for playing the winner ceremony */
	public final static byte WIN = 4;

	/** state for game over actions */
	public final static byte GAME_OVER = 5;
	
	/** state for highscore */
	public final static byte HIGHSCORE = 6;

    /** state for start */
    public final static byte START = 7;

    /** state for help */
	public static final byte HELP = 8;

	/**
	 * TODO: if necessary: add additional states e.g. sequences, resets,
	 * submenu, etc.
	 */

	/** current game state, start with menu **/
	public static byte currentGameState = INTRO;

	/**
	 * init the game settings (images, etc.)
	 */
        static Intro intro = null;
	static Arena arena;
	static Menu menu;
	static Highscore highscore;
	static GameOverScreen gameover;
    static Start_Level start;
    static MidiPlayer midiPlayer;
    static WavPlayer wavPlayer;
    static HelpScreen help;
    
    static boolean starHolder = false;
    static boolean played = false;

	public final static void init() {
            arena = null;
            menu = null;
            highscore = null;
            gameover = null;
            start = null;
            help = null;
            midiPlayer = new MidiPlayer();
            wavPlayer = new WavPlayer();
	}

	/**
	 * draw the game
	 * 
	 * @param g
	 *            Graphic Object
	 */

	public final static void drawGame(Graphics g) {
			LUMM2_Canvas.clearScreen(255, 255, 255, g);
			if (LUMM2_Canvas.star && !starHolder) {
				starHolder = true;
				midiPlayer.togglePause();
			}
			if (!LUMM2_Canvas.star && starHolder) {
				starHolder = false;
			}
            switch (currentGameState) {
                case SETTINGS:
                    // TODO: settings controls
                    // dummy code
                    break;
                case INTRO:
                    if (intro == null) {
                        highscore = null;
                        arena = null;
                        gameover = null;
                        start = null;
                        help = null;
                        System.gc();
                        intro = new Intro();
                        intro.start();
                    }
                    break;
                case MENU:
                	if(!played){
                		midiPlayer.play("/Computer_Love.mid");
                		played = true;
                	}
                	if (menu == null) {
                		menu = new Menu();
                		highscore = null;
                		arena = null;
                		gameover = null;
                		start = null;
                		help = null;
                		System.gc();
                		menu = new Menu();
                	}
                    menu.start();
                    break;
                case PLAY:
                	if (arena == null) {
                		menu = null;
                		highscore = null;
                		gameover = null;
                		start = null;
                		help = null;
                		System.gc();
                		arena = new Arena(1);
                	}
                    arena.logic();
                    if (currentGameState == PLAY) arena.draw(g);
                    break;
                case WIN:
                	if (gameover == null) {
                		menu = null;
                		highscore = null;
                		start = null;
                		help = null;
                		System.gc();
                		gameover = new GameOverScreen();
                	}
                    break;
                case GAME_OVER:
                	if (gameover == null) {
                		menu = null;
                		highscore = null;
                		start = null;
                		help = null;
                		System.gc();
                		gameover = new GameOverScreen();
                	}
                    gameover.start();
                    break;
                case HIGHSCORE:
                	if (highscore == null) {
                		menu = null;
                		start = null;
                		help = null;
                		arena = null;
                		System.gc();
                		highscore = new Highscore();
                	}
                    highscore.start();
                    break;
                case START:
                	if (arena == null) {
                		arena = new Arena(1);
                	}
                	if (start == null) {
                		start = new Start_Level();
                		menu = null;
                		help = null;
                		highscore = null;
                	}
                    start.start();
                    break;
                case HELP:
                	if (help == null) {
                		help = new HelpScreen();
                		menu = null;
                		highscore = null;
                		start = null;
                	}
                	help.logic();
                	help.draw(g);
                	break;
            }
	}
}
